package com.titaniume.rqbbitmq.one;

import com.rabbitmq.client.*;
import java.lang.String;
/**
 * @ProjectName: rqbbitmq
 * @Package: com.titaniume.rqbbitmq.one
 * @ClassName: Consumer
 * @Author: huangrenfei
 * @Description: 消费者
 * @Date: 2021/12/16 10:09
 * @Version: 1.0
 */
public class Consumer {

    //队列名称
    public static  final String QUEUE_NAME ="mirrior_hello";

    public static void main(String[] args) throws  Exception{

        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();

        //2.工厂Ip连接RabbitMq的队列
        connectionFactory.setHost("192.168.70.132");
        //用户名
        connectionFactory.setUsername("admin");
        //密码
        connectionFactory.setPassword("admin");
        //3.创建连接
        Connection connection = connectionFactory.newConnection();

        //4.创建信道
        Channel channel = connection.createChannel();

        //声明接收消息
        DeliverCallback deliverCallback =(consumerTag,message)->{
            System.out.println(new String(message.getBody()));
        };

        //取消消息的回调
        CancelCallback cancelCallback = consumerTag->{
            System.out.println("消息消费被中断");
        };



       //5.消费消息
        /**
         *
         * 1.消费哪个队列
         * 2.消费成功之后 是否要自动应答 true代表是自动应答 false代表手动应答
         * 3.消息发送过来后的回调接口
         * 4.消费者取消消费的回调
         */
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
    }

}
